<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/27
 * Time: 11:07
 */
namespace common\sources\read\taskcommod;

use common\models\craftsman\TaskCommod;

class TaskCommodAccess implements ITaskCommodAccess
{
    public function getTaskCommodById($task_commod_id)
    {
        return TaskCommod::find()
                ->select("id,pay_task_commisse,pay_task_amount,com_task_commisse,plat_type,task_type,shop_id,shop_name,port_type,commod_link,title,main_url1,main_url2,colour,size,price,num,range_price,ship_addr,qr_code_url,
                task_answer,task_num,keywords,task_status,margin,item_commisse,buy_get_commisse,service_amount,bus_id,create_time,public_task_status,task_id,is_task_image,img_praise_type,img_prase_type_pepper,freight_deposit,surplus_task_num,
                task_img_pepper,prompt,com_amount,freight_deposit,total_amount,item_task_amount")
                ->where(['id' => $task_commod_id])
                ->asArray()
                ->one();
    }
    //
    public function taskCommodCount($data)
    {
        $where = $this->getWhere($data);
        $taskcommod = TaskCommod::find()
                ->alias("t");
        if($data['search_type'] == 2 || $data['search_type'] == 3 || $data['search_type'] == 4 )
        {
            $taskcommod->leftJoin("task_order o","t.id = o.task_commod_id");
            $taskcommod->groupBy("t.id");
        }
        $taskcommod->where($where);
        return $taskcommod->count();
    }

    public function taskCommodCompleteCout($data)
    {
        $data['task_status'] = 6;
        $where = $this->getWhere($data);
        $taskcommod = TaskCommod::find()
            ->alias("t");
        if($data['search_type'] == 2 || $data['search_type'] == 3 || $data['search_type'] == 4 )
        {
            $taskcommod->leftJoin("task_order o","t.id = o.task_commod_id");
            $taskcommod->groupBy("t.id");
        }
        $taskcommod->where($where);
        return $taskcommod->count();
    }

    public function taskCommodHavingCout($data)
    {
        $data['task_status'] = 4;
        $where = $this->getWhere($data);
        $taskcommod = TaskCommod::find()
            ->alias("t");
        if ($data['search_type'] == 2 || $data['search_type'] == 3 || $data['search_type'] == 4) {
            $taskcommod->leftJoin("task_order o", "t.id = o.task_commod_id");
            $taskcommod->groupBy("t.id");
        }
        $taskcommod->where($where);
        return $taskcommod->count();

    }
        public function taskCommodList($data)
    {
        $where = $this->getWhere($data);
        $taskcommod = TaskCommod::find()
            ->alias("t");
        $taskcommod->select("t.id,t.shop_name,t.title,t.main_url1,t.size,t.colour,t.task_id,t.create_time,t.task_status,t.task_num,t.task_type,t.plat_type,t.reason");
        if($data['search_type'] == 2 || $data['search_type'] == 3 || $data['search_type'] == 4 )
        {
            $taskcommod->leftJoin("task_order o","t.id = o.task_commod_id");
            $taskcommod->groupBy("t.id");
        }
        $taskcommod->where($where);
        $taskcommod->orderBy("t.create_time desc");
        $taskcommod->offset(($data['page_num']-1) * $data['limit']);
        $taskcommod->limit($data['limit']);
        return $taskcommod->asArray()->all();

    }
    private function getWhere($data)
    {
        $sql = " t.bus_id = " .$data['bus_id'];
        if($data['task_commod_status'] ==1)
        {
            $sql .= " AND t.task_status = 4";
        }else if($data['task_commod_status'] == 2)
        {
            $sql .= " AND t.task_status = 6";
        }
        if(!empty($data['task_status']) && isset($data['task_status']))
        {
            $sql .= " AND t.task_status = " .$data['task_status'];
        }
        if(!empty($data['plat_type']) && isset($data['plat_type']))
        {
            $sql .= " AND t.plat_type = " .$data['plat_type'];
        }
        if(!empty($data['shop_id']) && isset($data['shop_id']))
        {
            $sql .= " AND t.shop_id = " .$data['shop_id'];
        }
        if(!empty($data['start_time']) && isset($data['start_time']))
        {
            $sql .= " AND t.create_time >= " .$data['start_time'];
        }
        if(!empty($data['end_time']) && isset($data['end_time']))
        {
            $sql .= " AND create_time <= " .$data['end_time'];
        }
        if(!empty($data['search_type']) && isset($data['search_type']))
        {
            if($data['search_type'] == 1 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND t.task_id = '" .$data['search_text']."'";
            }else if($data['search_type'] == 2 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND o.task_order_num = '" .$data['search_text']."'";
            }else if($data['search_type'] == 3 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND o.pay_order_num = '" .$data['search_text']."'";
            }else if($data['search_type'] == 4 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND o.tbname = '" .$data['search_text']."'";
            }else if($data['search_type'] == 5 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND t.title = '" .$data['search_text']."'";
            }
        }
        return $sql;
    }
}